package com.thingcom.chinamobile.service;

import cn.hutool.core.codec.Base64;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.ctg.ag.sdk.biz.AepDeviceManagementClient;
import com.ctg.ag.sdk.biz.AepDeviceStatusClient;
import com.ctg.ag.sdk.biz.aep_device_management.CreateDeviceRequest;
import com.ctg.ag.sdk.biz.aep_device_management.CreateDeviceResponse;
import com.ctg.ag.sdk.biz.aep_device_status.GetDeviceStatusHisInPageRequest;
import com.ctg.ag.sdk.biz.aep_device_status.GetDeviceStatusHisInPageResponse;
import com.ctg.ag.sdk.core.model.BaseApiRequest;
import com.ctg.ag.sdk.core.model.BaseApiResponse;
import com.thingcom.base.util.RRException;
import com.thingcom.chinamobile.dto.DataDto;
import com.thingcom.chinamobile.factory.ApiClientFactory;
import com.thingcom.chinamobile.handler.BaseAepClientWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.io.UnsupportedEncodingException;

/**
 * @ClassName DeviceStatusServiceImpl
 * @Description 设备状态查询服务实现
 * @Author zhou tao
 * @Date 2019/12/17 19:59
 * @Version 1.0
 **/
@Service
@Slf4j
public class DeviceStatusServiceImpl implements DeviceStatusService {

	@Override
	public JSONObject getDeviceStatusHisInPage(DataDto dataDto) {
		AepDeviceStatusClient client = (AepDeviceStatusClient) ApiClientFactory.getInstance(dataDto.getAppKey() + "AepDeviceStatusClient");
		GetDeviceStatusHisInPageRequest request = new GetDeviceStatusHisInPageRequest();
		JSONObject result = new BaseAepClientWrapper() {
			@Override
			public BaseApiResponse handleRequest(BaseApiRequest request) throws Exception {
				return client.getDeviceStatusHisInPage((GetDeviceStatusHisInPageRequest)request);
			}
		}.excute(request, JSONUtil.parseObj(dataDto));

		JSONArray datas = result.getJSONArray("deviceStatusList");
		for (JSONObject data : datas.jsonIter()) {
			data.put("APPdata", JSONUtil.toJsonStr(Base64.decodeStr(data.getStr("APPdata"))));
		}

		return result;
	}
}
